﻿using System.ComponentModel;
using System.Windows.Forms;

namespace Grinder.Components
{
    public sealed partial class EnhancedListView : ListView
    {
        public EnhancedListView()
        {
            InitializeComponent();
            DoubleBuffered = true;
        }

        public EnhancedListView(IContainer container)
            : this()
        {
            container.Add(this);
        }

        public void RemoveSelected()
        {
            BeginUpdate();

            foreach (ListViewItem item in SelectedItems)
                item.Remove();

            EndUpdate();
        }

        public void MoveSelectedUp()
        {
            MoveListItems(MoveDirection.Up);
        }

        public void MoveSelectedDown()
        {
            MoveListItems(MoveDirection.Down);
        }

        private void MoveListItems(MoveDirection direction)
        {
            if (SelectedItems.Count == 0)
                return;

            if (direction == MoveDirection.Up)
            {
                if (SelectedIndices[0] == 0)
                    return;
            }
            else
            {
                int lastIdx = SelectedIndices.Count - 1;

                if (SelectedIndices[lastIdx] == Items.Count - 1)
                    return;
            }

            BeginUpdate();

            if (direction == MoveDirection.Up)
                foreach (int index in SelectedIndices)
                    SwapItems(index, direction);
            else
                for (int i = (SelectedIndices.Count - 1); i >= 0; --i)
                    SwapItems(SelectedIndices[i], direction);

            EndUpdate();
        }

        private void SwapItems(int index, MoveDirection direction)
        {
            if (direction == MoveDirection.Up)
            {
                ListViewItem it1 = Items[index];
                ListViewItem it2 = Items[index - 1];

                Items.RemoveAt(index);
                Items.RemoveAt(index - 1);

                Items.Insert(index - 1, it1);
                Items.Insert(index, it2);
            }
            else
            {
                ListViewItem it1 = Items[index];
                ListViewItem it2 = Items[index + 1];

                Items.RemoveAt(index + 1);
                Items.RemoveAt(index);

                Items.Insert(index, it2);
                Items.Insert(index + 1, it1);
            }
        }
        
        private void EnhancedListView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.A)
            {
                SendKeys.Send("{HOME}");
                SendKeys.Send(" ");
                SendKeys.Send("+{end}");
            }
        }

        private void EnhancedListView_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                RemoveSelected();
            }
        }

        private enum MoveDirection
        {
            Up,
            Down
        }
    }
}